//
//  CSChoiPayuSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/7.
//

import UIKit
import SwiftyStoreKit
import JDStatusBarNotification


class CSChoiPayuSweeping: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource{
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    
    private var cakeCSB:Dictionary<String,CGPoint>?
    private var fookCSB: UITextField?

    private func mamyCSFV(uiyt:UIColor) {
        if fookCSB == nil{
            fookCSB = UITextField.init()
            fookCSB?.backgroundColor = uiyt
            fookCSB?.leftViewMode = .never
            fookCSB?.placeholder = nil
        }
        
        
        if cakeCSB == nil{
            cakeCSB = Dictionary.init()
        }
        
        var poinrtCSB = CGPoint.init(x: 23, y: 34)
        
        cakeCSB?["\(CGFloat.random(in: 0...5))"] = poinrtCSB
        
        if fookCSB?.text == nil {
            cakeCSB?["\(CGFloat.random(in: 5...20))"] = poinrtCSB
        }
        poinrtCSB.y += 10
        
        if cakeCSB?.keys.count ?? 0 > 0 {
            
        }
        
        if poinrtCSB.y > 2 && poinrtCSB.x > 2 {
            
        }
        
    }
    
    var selitemTag:Int = 0
    

    let alllpayIDCSB = [("kfjrqnnqxocflepb","$0.99",400,560000),
                     ("biweszoeilytsbgs","$1.99",800,560000),
                     ("tlmmfhlksivndxai","$4.99",2450,560000),
                     ("rvzrbfaqfnwxzrox","$9.99",4900,560000),
                     ("lzofkqenkgnahgfy","$19.99",9800,560000),
                     ("mefusbmssgedxvtm","$49.99",24500,560000),
                     ("ytdeolwwgwygcbtw","$99.99",49000,560000)]

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selitemTag = indexPath.row
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return 7
        }
        return 7
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let celll = collectionView.dequeueReusableCell(withReuseIdentifier: "CSBPauijCellID", for: indexPath) as! CSBPauijCell
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            celll.topCountrCSBbg.text = "\(alllpayIDCSB[indexPath.row].2)"
            celll.bottomgMOneyCSBbg.text = alllpayIDCSB[indexPath.row].1
        }else{
            celll.topCountrCSBbg.text = "\(alllpayIDCSB[indexPath.row].2)"
            celll.bottomgMOneyCSBbg.text = alllpayIDCSB[indexPath.row].1
        }
        
        return celll
        
    }
    

    @IBOutlet weak var coinDataSource: UICollectionView!
    
    
    @IBOutlet weak var databootombgCSB: UIView!
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        databootombgCSB.layer.cornerRadius = 40
        databootombgCSB.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
       
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       if var balanve = Int(CSBFetcherAll.Hammer.melingCSBINfo["allCoind"] ?? "0"){
            
           
            
            self.mineBlanceCSB.text = "\(balanve)"
            
        }
        
        
        if csbColoter == UIColor.black {
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe)
            view.backgroundColor = csbColoter
        }else{
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe + 1)
        }
        guard csbHeihei != nil else { return  }

        if view.backgroundColor == csbColoter {
            let qurk = UIButton(configuration: UIButton.Configuration.plain())
            qurk.isHidden = true
            qurk.isEnabled = false
            view.addSubview(qurk)
        }
        coinDataSource.delegate = self
        coinDataSource.dataSource = self
        coinDataSource.register(CSBPauijCell.self, forCellWithReuseIdentifier: "CSBPauijCellID")
        
        let flokaliout = UICollectionViewFlowLayout.init()
        
        flokaliout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 30.xQQ - 26.xQQ)/3, height: 130)
        flokaliout.scrollDirection = .vertical
        flokaliout.minimumLineSpacing = 12.xQQ
        flokaliout.minimumInteritemSpacing = 10.xQQ
        coinDataSource.selectItem(at: IndexPath.init(row: 0, section: 0), animated: false, scrollPosition: .top)
        coinDataSource.collectionViewLayout = flokaliout
        coinDataSource.contentInset = UIEdgeInsets(top: 0, left: 13.xQQ, bottom: 100, right: 13.xQQ)
    }

    @IBOutlet weak var mineBlanceCSB: UILabel!
    
    @IBAction func fanghUInow(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        self.navigationController?.popViewController(animated: true)
    }


    @IBAction func vcsbSurePay(_ sender: UIButton) {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
       
        view.isUserInteractionEnabled = false
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
       
        SwiftyStoreKit.purchaseProduct(self.alllpayIDCSB[selitemTag].0, atomically: true) { psResult in
            if cabSunmAll > 5 {
                csbBayui.append(contentsOf: csbNUmber)
            }else{
                csbBayui.append(contentsOf: csbNumbe2)
            }

            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

           
            if case .success(let psPurch) = psResult {
                let psdownloads = psPurch.transaction.downloads
                
                if !psdownloads.isEmpty {
                    SwiftyStoreKit.start(psdownloads)
                }
                
                if psPurch.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(psPurch.transaction)
                }
                
                self.view.isUserInteractionEnabled = true
              
                
                if var balanve = Int(CSBFetcherAll.Hammer.melingCSBINfo["allCoind"] ?? "0"){
                    
                    balanve = balanve + self.alllpayIDCSB[self.selitemTag].2
                    CSBFetcherAll.Hammer.melingCSBINfo["allCoind"] = "\(balanve)"
                    
                    self.mineBlanceCSB.text = "\(balanve)"
                }
                if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                    NotificationPresenter.shared.dismiss()
                    NotificationPresenter.shared.present("Successful payment!", includedStyle: .success)
                    self.navigationController?.popViewController(animated: true)
                }
               
          
            }else if case .error(let error) = psResult {
                self.view.isUserInteractionEnabled = true
                NotificationPresenter.shared.dismiss()
               
                if error.code != .paymentCancelled {
                    NotificationPresenter.shared.present(error.localizedDescription, includedStyle: .warning)
                }
            }
        }
    }
}





